package com.selfstudy.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.selfstudy.common.core.domain.entity.SysDictData;
import com.selfstudy.common.exception.ServiceException;
import com.selfstudy.common.utils.DateUtils;
import com.selfstudy.system.domain.SRoom;
import com.selfstudy.system.mapper.SRoomMapper;
import com.selfstudy.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.selfstudy.system.mapper.SOrderMapper;
import com.selfstudy.system.domain.SOrder;
import com.selfstudy.system.service.ISOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 预约信息Service业务层处理
 *
 * @author lgf
 * @date 2024-01-30
 */
@Service
public class SOrderServiceImpl implements ISOrderService {
    @Resource
    private SOrderMapper sOrderMapper;
    @Resource
    private SRoomMapper sRoomMapper;
    @Resource
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public SOrder selectSOrderById(Long id) {
        return sOrderMapper.selectSOrderById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param sOrder 预约信息
     * @return 预约信息
     */
    @Override
    public List<SOrder> selectSOrderList(SOrder sOrder) {
        List<SRoom> sRooms = sRoomMapper.selectSRoomList(new SRoom());
        Map<Long, String> roomMap = sRooms.stream().collect(Collectors.toMap(SRoom::getRoomId, SRoom::getRoomName));
        List<SysDictData> building = dictDataMapper.selectDictDataByType("teaching_building");
        Map<String, String> buildMap = building.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<SOrder> sOrders = sOrderMapper.selectSOrderList(sOrder);
        sOrders.forEach(order -> {
            if (!Objects.isNull(order.getRoomId())){
                order.setRoomName(roomMap.get(Long.valueOf(order.getRoomId())));
            }
            order.setFloorName(buildMap.get(order.getFloorId()));
        });

        return sOrders;
    }

    /**
     * 新增预约信息
     *
     * @param sOrder 预约信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSOrder(SOrder sOrder) {
        sOrder.setCreateTime(DateUtils.getNowDate());
        if (Objects.isNull(sOrder.getSeatXy())){
            throw new ServiceException("seatXy不能为空");
        }
        if (Objects.isNull(sOrder.getRoomId())){
            throw new ServiceException("roomId不能为空");
        }
        updateRoom(sOrder,1);
        return sOrderMapper.insertSOrder(sOrder);
    }

    public void updateRoom(SOrder sOrder,Integer status){
        SRoom sRoom = sRoomMapper.selectSRoomByRoomId(Long.valueOf(sOrder.getRoomId()));
        String json = sRoom.getCoordinateJson();
        JSONArray coordinateArray = JSONArray.parse(json);

        String[] split = sOrder.getSeatXy().split(",");
        if (split.length!=2){{
            throw new ServiceException("坐标格式错误逗号分割的row,column");
        }}
        int row = Integer.parseInt(split[0]);
        int column = Integer.parseInt(split[1]);
        JSONArray rowArray = JSONArray.from(coordinateArray.get(row));
        JSONObject columnJSON = rowArray.getJSONObject(column);
        columnJSON.put("status",status);
        rowArray.set(column,columnJSON);
        coordinateArray.set(row,rowArray);
        sRoom.setCoordinateJson(coordinateArray.toJSONString());
        sRoomMapper.updateSRoom(sRoom);
    }

    /**
     * 修改预约信息
     *
     * @param sOrder 预约信息
     * @return 结果
     */
    @Override
    public int updateSOrder(SOrder sOrder) {
        sOrder.setUpdateTime(DateUtils.getNowDate());
        return sOrderMapper.updateSOrder(sOrder);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSOrderByIds(Long[] ids) {
        for (Long id : ids) {
            SOrder sOrder = sOrderMapper.selectSOrderById(id);
            updateRoom(sOrder,0);
        }
        return sOrderMapper.deleteSOrderByIds(ids);
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSOrderById(Long id) {
        SOrder sOrder = sOrderMapper.selectSOrderById(id);
        updateRoom(sOrder,0);
        return sOrderMapper.deleteSOrderById(id);
    }
}
